package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.system.entity.MsgEntity;
import  com.sq.universe.system.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/msg")
@CrossOrigin
@Api(description = "代碼生產")
public class MsgController extends BaseController {

    @Autowired
    private MsgService msgService;

    /**
    * 查询数据
    * @param msgEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:msg:query")
    public RespEntity query(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            return new RespEntity("查询成功",msgService.query(msgEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param msgEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:msg:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            PageHelper.startPage(msgEntity.getPageNo(),msgEntity.getPageSize());
            List<MsgEntity> msgList = msgService.query(msgEntity);
            PageInfo<MsgEntity> pageInfo = new PageInfo<MsgEntity>(msgList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param msgEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:msg:insert")
    public RespEntity insert(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            msgEntity.setMsgCreateTime(new Date());
            msgEntity.setMsgCreateUser(this.getCurrentUser().getUserId());
            msgService.insert(msgEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param msgEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:msg:update")
    public RespEntity update(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            msgEntity.setMsgUpdateTime(new Date());
            msgEntity.setMsgUpdateUser(this.getCurrentUser().getUserId());
            msgService.update(msgEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param msgEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:msg:delete")
    public RespEntity delete(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            if(msgEntity.getMsgId() == null){
                return new RespEntity("删除失败");
            }
            msgEntity.setMsgUpdateTime(new Date());
            msgEntity.setMsgUpdateUser(this.getCurrentUser().getUserId());
            msgService.logicalDelete(msgEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }

    /**
    * 根据ID查询
    * @param msgEntity
    * @return
    */
    @PostMapping("/getById")
    @RequiresPermissions("system:msg:getById")
    public RespEntity getById(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            if(msgEntity.getMsgId() == null){
                return new RespEntity("请输入id查询");
            }
            return new RespEntity("查询成功",msgService.getById(msgEntity.getMsgId()));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 根据实体查询
    * @param msgEntity
    * @return
    */
    @PostMapping("/get")
    @RequiresPermissions("system:msg:get")
    public RespEntity get(@RequestBody(required = false) MsgEntity msgEntity){
        try {
            return new RespEntity("查询成功",msgService.get(msgEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }
}